//
//  MineTopVM.swift
//  kxf
//
//  Created by Mac on 2024/3/5.
//

import Foundation
import UIKit

class MineTopVM: UIView {
    
    let selfHeight = kFitWidth(354) + statusBarHeight
    
    var authenTapBlock:(()->())?

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .clear
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var bgImgView : UIImageView = {
        let img = UIImageView()
        img.image = UIImage.init(named: "mine_top_bg_img")
        
        return img
    }()
    lazy var titleLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .white
        lab.font = .systemFont(ofSize: 18, weight: .medium)
        lab.text = "我的"
        
        return lab
    }()
    lazy var headImgView : UIImageView = {
        let img = UIImageView()
        img.image = UIImage.init(named: "avatar_default")
        
        return img
    }()
    lazy var nameLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .white
        lab.font = .systemFont(ofSize: 16, weight: .medium)
        lab.adjustsFontSizeToFitWidth = true
        return lab
    }()
    lazy var phoneLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .white
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        lab.adjustsFontSizeToFitWidth = true
        return lab
    }()
    lazy var channelNameLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .white
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        lab.adjustsFontSizeToFitWidth = true
        return lab
    }()
    lazy var authenStatusBgImgView : UIImageView = {
        let img = UIImageView()
//        img.setImgLocal(imgName: "mine_authen_bg")
        img.setImgLocal(imgName: "mine_top_item_bg")
        img.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(authenImgTapAction))
        img.addGestureRecognizer(tap)
        
        return img
    }()
    lazy var authenStatusLabel : UILabel = {
        let lab = UILabel()
        lab.text = "待提交，提交后可享受更多特权"
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        lab.textColor = .black
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
    lazy var authenStatusLab : UILabel = {
        let lab = UILabel()
        lab.text = "去认证"
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        lab.textColor = .THEME
        
        return lab
    }()
    lazy var arrowImgView : UIImageView = {
        let img = UIImageView()
        img.image = UIImage.init(named: "login_arrow_img")
        img.isUserInteractionEnabled = true
        
        return img
    }()
    lazy var todayNumberVm: MineTodayNumberVM = {
        let vm = MineTodayNumberVM.init(frame: CGRect.init(x: 0, y: WHBaseViewVC().getNavigationBarHeight()+kFitWidth(136), width: 0, height: 0))
        return vm
    }()
}

extension MineTopVM{
    func updateUI() {
        self.phoneLabel.text = UserModel.shared.phoneStar
        self.nameLabel.text = "\(UserModel.shared.realName)" //"用户名：\(UserModel.shared.realName)" 
        self.channelNameLabel.text = UserModel.shared.posChannelName
        self.authenStatusLab.text = "去认证"
        if UserModel.shared.posStatus == "-4"{
            self.authenStatusLabel.text = "复审驳回,认证后可享受更多特权功能"
            self.authenStatusLab.text = "查看原因"
        }else if UserModel.shared.posStatus == "-3"{
            self.authenStatusLabel.text = "复审待审核,审核通过后可享受更多特权功能"
            self.authenStatusLab.text = "查看原因"
        }else if UserModel.shared.posStatus == "-2"{
            self.authenStatusLabel.text = "初审驳回,认证后可享受更多特权功能"
            self.authenStatusLab.text = "查看原因"
        }else if UserModel.shared.posStatus == "-1"{
            self.authenStatusLabel.text = "初审驳回,认证后可享受更多特权功能"
            self.authenStatusLab.text = "查看原因"
        }else if UserModel.shared.posStatus == "0"{
            self.authenStatusLabel.text = "待提交,提交后可享受更多特权功能"
            self.authenStatusLab.text = "去认证"
        }else if UserModel.shared.posStatus == "1"{
            self.authenStatusLabel.text = "审核中,审核后可享受更多特权功能"
            self.authenStatusLab.text = "审核中"
        }else if UserModel.shared.posStatus == "2"{
            self.authenStatusLabel.text = "待授权,授权后可享受更多特权功能"
            self.authenStatusLab.text = "去授权"
        }else if UserModel.shared.posStatus == "3"{
            self.authenStatusLabel.text = "已绑定，享受多项特权功能"
            self.authenStatusLab.text = "去授权"
        }else if UserModel.shared.posStatus == "4"{
            self.authenStatusLabel.text = "已绑定，享受多项特权功能"
            self.authenStatusLab.text = "已认证"
        }
    }
    @objc func authenImgTapAction() {
        if self.authenTapBlock != nil{
            self.authenTapBlock!()
        }
    }
}

extension MineTopVM{
    func initUI() {
        addSubview(bgImgView)
        addSubview(titleLabel)
        addSubview(headImgView)
        addSubview(nameLabel)
        addSubview(phoneLabel)
        addSubview(channelNameLabel)
        addSubview(authenStatusBgImgView)
        authenStatusBgImgView.addSubview(authenStatusLabel)
        authenStatusBgImgView.addSubview(authenStatusLab)
        authenStatusBgImgView.addSubview(arrowImgView)
        
        addSubview(todayNumberVm)
        
        setConstrait()
    }
    func setConstrait() {
        bgImgView.snp.makeConstraints { make in
            make.left.top.width.height.equalToSuperview()
        }
        titleLabel.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(statusBarHeight+kFitWidth(10))
//            make.height.equalTo(kFitWidth(44))
        }
        headImgView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(34))
            make.top.equalTo(titleLabel.snp.bottom).offset(kFitWidth(5))
            make.width.height.equalTo(kFitWidth(80))
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImgView.snp.right).offset(kFitWidth(16))
            make.top.equalTo(headImgView.snp.top).offset(kFitWidth(9))
            make.right.equalTo(kFitWidth(-20))
        }
        phoneLabel.snp.makeConstraints { make in
            make.left.right.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(kFitWidth(9))
        }
        channelNameLabel.snp.makeConstraints { make in
            make.left.equalTo(phoneLabel)
            make.top.equalTo(phoneLabel.snp.bottom).offset(kFitWidth(7))
        }
        authenStatusBgImgView.snp.makeConstraints { make in
            make.width.equalTo(kFitWidth(347))
            make.height.equalTo(kFitWidth(34))
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(headImgView.snp.bottom).offset(kFitWidth(18))
        }
        authenStatusLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(9))
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(kFitWidth(-100))
        }
        authenStatusLab.snp.makeConstraints { make in
            make.right.equalTo(arrowImgView.snp.left).offset(kFitWidth(-8))
            make.centerY.lessThanOrEqualToSuperview()
        }
        arrowImgView.snp.makeConstraints { make in
            make.centerY.lessThanOrEqualToSuperview()
            make.width.height.equalTo(kFitWidth(20))
            make.right.equalTo(kFitWidth(-8))
        }
    }
}
